const express = require("express");
const router = express.Router();
const handle = require("../routes_handle/comments"); // 假设业务逻辑在这里
const { CODE, Response } = require("../utils/response");
// 1. 获取文档评论
router.get("/comment/getComments", async (req, res, next) => {
  try {
    const { FDocId } = req.query;
    if (!FDocId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少文档ID"));
    }
    const result = await handle.getComments(FDocId);
    res.status(CODE.SUCCESS).json(Response.success(result, "获取成功"));
  } catch (err) {
    next(err);
  }
});

// 2. 添加评论
router.post("/comment/addComment", async (req, res, next) => {
  try {
    const { FDocId, FUserId, FContent } = req.body;
    if (!FDocId || !FUserId || !FContent) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少必要参数"));
    }
    const result = await handle.addComment({ FDocId, FUserId, FContent });
    res.status(CODE.SUCCESS).json(Response.success(result, "添加成功"));
  } catch (err) {
    next(err);
  }
});

// 3. 删除评论
router.post("/comment/deleteComment", async (req, res, next) => {
  try {
    const { FId } = req.body;
    if (!FId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少评论ID"));
    }
    const result = await handle.deleteComment(FId);
    res.status(CODE.SUCCESS).json(Response.success(result, "删除成功"));
  } catch (err) {
    next(err);
  }
});

module.exports = router;
